const mongoose = require('mongoose');

const tagSchema = mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
      trim: true,
    },
    description: {
      type: String,
      trim: true,
      default: '',
    },
    count: {
      type: Number,
      default: 0,
    },
    isDeleted: {
      type: Boolean,
      default: false,
    },
  },
  {
    timestamps: true,
  }
);

/**
 * 检查标签名是否已被使用
 * @param {string} name - 标签名
 * @param {ObjectId} [excludeTagId] - 排除的标签ID
 * @returns {Promise<boolean>}
 */
tagSchema.statics.isNameTaken = async function (name, excludeTagId) {
  const tag = await this.findOne({ name, _id: { $ne: excludeTagId }, isDeleted: false });
  return !!tag;
};

/**
 * @typedef Tag
 */
const Tag = mongoose.model('Tag', tagSchema, 'tags');

module.exports = Tag; 